package cn.chencaiju.blog.service;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import cn.chencaiju.blog.domain.AjaxResponse;
import cn.chencaiju.blog.domain.Post;
import cn.chencaiju.blog.mapper.PostMapper;
import cn.chencaiju.blog.utils.SessionFactoryUtils;

public class PostService {
	private SqlSessionFactory factory ;
	private SqlSession session;
	private PostMapper mapper;
	
	private void initlized() {
		factory = SessionFactoryUtils.getFactory();
		session = factory.openSession();
		mapper = session.getMapper(PostMapper.class);
	}
	public AjaxResponse<Post> page(){
		initlized();
		List<Post> list = mapper.selectAll();
		int count = mapper.count();
		AjaxResponse<Post> ajax=new AjaxResponse<>();
		ajax.setData(list);
		ajax.setCount(count);
		ajax.setCode(0);
		ajax.setMsg("successed");

		session.close();
		return ajax;
	}
}
